/**
 * Created by Administrator on 2016/9/27.
 */
angular.module("redBookAPP.controller",[])
    .controller("TabsController",function($scope){
        
    })
    .controller("HomeController",function ($scope, $ionicPopover, HomeFactory) {
    	$(".button").click(function (){
               $(this).addClass("button-select").siblings().removeClass("button-select");
               });
        $ionicPopover.fromTemplateUrl("pop.html",{})
                    .then(function (popover) {
                        $scope.popover=popover;
                    });
        $scope.showMenu=function (event) {
            $scope.popover.show(event);
        };
        //数据监听
        $scope.$on('updateHome',function () {
            $scope.HomeData = HomeFactory.getHomeData();
            console.log($scope.HomeData);
        });
        //发起请求
        HomeFactory.getHome(1);

        $scope.selectPage=function (i) {
            var btns = $(".sub_header_list a");
            $.each(btns,function (index,ele) {
                ele.className = 'button button-clear';
            });
            btns[i-1].className = 'button button-clear button-select';
            // HomeFactory.getUrl(i);
            if(i!=2){
                HomeFactory.getHome(i);
            }
            else {
                alert("你还没有登录！");
            }
        }
    })
    //笔记详情
    .controller("XiangqingController",function ($scope, $stateParams, $rootScope, XiangpingFactory, $ionicSlideBoxDelegate) {
        //进入详情页，隐藏tabs
        $rootScope.hideTabs = 'tabs-item-hide';
        //当退出详情页时，页面会销毁释放
        //监听销毁事件
        $scope.$on('$destroy',function () {
            $rootScope.hideTabs ='';
            alert("你好");
        });
        
        console.log($stateParams.id);
        
        //数据监听
        $scope.$on('updateHome',function () {
            $scope.XiangqingData = XiangpingFactory.getXiangqingData();
            // console.log($scope.XiangqingData_xiao);
            $scope.count=$scope.XiangqingData.images_list.length;
            $scope.huanye = function (gg) {
                $scope.dangqianye=$ionicSlideBoxDelegate.currentIndex();
                $scope.count=$ionicSlideBoxDelegate.slidesCount();
            };
            $ionicSlideBoxDelegate.update();
        });
        $scope.$on('updateHome_s',function () {
            $scope.XiangqingData_xiao = XiangpingFactory.getXiangqingData_xia();
        });
        //发起请求
        XiangpingFactory.getXiangqing();
        XiangpingFactory.getXiangqing_xia(1);

        $scope.dianji=function (index) {
            // alert(index);
            XiangpingFactory.getXiangqing_xia(index);
        }
    })
    //评论
    .controller("PinglunController",function ($scope, $rootScope, $stateParams, PinglunFactory) {
        console.log($stateParams.id);

        //进入详情页，隐藏tabs
        // $rootScope.hideTabs = 'tabs-item-hide';
        //当退出详情页时，页面会销毁释放
        //监听销毁事件
        // $scope.$on('$destroy',function () {
        //     $rootScope.hideTabs ='';
        // });

        $scope.$on('updatePinglun',function () {
            $scope.PinglunData = PinglunFactory.getPinglunData();
            console.log($scope.PinglunData);
        });
        PinglunFactory.getPinglun();
    })
    //发现部分
    .controller("FindController",function ($scope,$ionicSlideBoxDelegate,FindFactory) {
    	 $scope.onClickPage=function (index) {
            $ionicSlideBoxDelegate.slide(index);
        };
//      $scope.onSlide=function ($index) {
//          console.log()
//      };
        $scope.$on('updateHots',function () {
            $scope.FindData = FindFactory.getFindData();
//        console.log($scope.FindData);
          $scope.findZhuanjiData = FindFactory.getFindZhuanjiData();
        console.log($scope.findZhuanjiData);
        });
        FindFactory.getFind();
        FindFactory.getFindZhuanji();
    })
    //购买部分
    .controller("PurchaseController",function ($scope,$state,$rootScope,PurchaseFactory) {

     	//数据监听
        $scope.$on('updatePurchase',function () {
            $scope.PurchaseData = PurchaseFactory.getPurchaseData();
            $scope.purchaseXiaData = PurchaseFactory.getPurchaseXiaData();
//          console.log($scope.PurchaseData);
			console.log($scope.purchaseXiaData);
        });

        //发起请求
        PurchaseFactory.getPurchase(0);
		$scope.purchasePage=function (i) {
               $(".button").click(function (){
               $(this).addClass("button-select").siblings().removeClass("button-select");
            });
           PurchaseFactory.getPurchase(i);
           PurchaseFactory.getPurchaseXia(i);
     }

//         PurchaseFactory.getPurchase(i);
//         PurchaseFactory.getPurchaseXia(i);	
    })
    //购买的子部分
    .controller("QuanbuController",function($scope){

    })
    .controller("HufuController",function($scope){

    })
    .controller("CaizhuangController",function($scope){

    })
    .controller("ShishangController",function($scope){

    })
    .controller("JiajuController",function($scope){

    })
    .controller("MuyingController",function($scope){

    })
    .controller("GerenhuliController",function($scope){

    })
    .controller("JiatingqingjieController",function($scope){

    })
    .controller("MeishiController",function($scope){

    })
    .controller("BaojianpinController",function($scope){

    })
    .controller("ShumajiadianController",function($scope){

    })
    //消息部分
    .controller("NewsController",function ($scope,NewsFactory,$rootScope) {
        $scope.$on('updateNews',function () {
            $rootScope.newsData = NewsFactory.getNewsData();
			console.log($rootScope.newsData);
			$scope.arrX = [];
			for(var j=0;j<$rootScope.newsData.length;j++){
				//转换时间
				var time=$rootScope.newsData[j].time;
				var d = new Date(time*1000);
                var zhouji = "周日";
                var arrzhouji = ["周日","周一","周二","周三","周四","周五","周六"];
                for(var i=0;i<=6;i++){
                    if(d.getDay()==i){
                        zhouji = arrzhouji[i];
                    }
                }
                var riqi = {
                    nian:d.getFullYear(),
                    yue:(d.getMonth()<10)?("0"+d.getMonth()):d.getMonth()+1,
                    ri:(d.getDate()<10)?("0"+d.getDate()):d.getDate(),
                    shi:(d.getHours()<10)?("0"+d.getHours()):d.getHours(),
                    fen:(d.getMinutes()<10)?("0"+d.getMinutes()):d.getMinutes(),
                    xingqi:zhouji
                };
                $scope.arrX.push(riqi);
			}
			
        });
        //发起请求
        NewsFactory.getNews(0);
		$scope.newsPage=function (i) {
             var btns = $(".nav-news a");
            $.each(btns,function (index,ele) {
                ele.className = 'tab-item';
            });
            btns[i].className = 'tab-item a-active';
           NewsFactory.getNews(i);
   }
    })
    .controller("DongtaiController",function($scope){
    	
    })
    .controller("TongzhiController",function($scope){
    	
    })
    .controller("GuanyuniController",function($scope){
    	
    })
    //我的部分
    .controller("MineController",function ($scope,$ionicSlideBoxDelegate,$ionicScrollDelegate) {
        var $buttonBar1 = $(".buttonBar1 .button");
        var $buttonBar2 = $(".buttonBar2 .button");
        var left = true;

       $scope.zhuoyouhuadong = function () {
           if(left){
               $buttonBar1.eq(1).addClass("mine-xiahuaxian").siblings().removeClass("mine-xiahuaxian");
               $buttonBar2.eq(1).addClass("mine-xiahuaxian").siblings().removeClass("mine-xiahuaxian");
               left = false;
           }
           else {
               $buttonBar1.eq(0).addClass("mine-xiahuaxian").siblings().removeClass("mine-xiahuaxian");
               $buttonBar2.eq(0).addClass("mine-xiahuaxian").siblings().removeClass("mine-xiahuaxian");
               left = true;
           }
           console.log(left);
       }

       $scope.huanyeSlide = function (index) {
           // console.log(index);
           if (index==1){
               console.log(index);
               $buttonBar1.eq(0).addClass("mine-xiahuaxian").siblings().removeClass("mine-xiahuaxian");
               $buttonBar2.eq(0).addClass("mine-xiahuaxian").siblings().removeClass("mine-xiahuaxian");
               $ionicSlideBoxDelegate.previous();
           }
           else{
               console.log(index);
               $buttonBar1.eq(1).addClass("mine-xiahuaxian").siblings().removeClass("mine-xiahuaxian");
               $buttonBar2.eq(1).addClass("mine-xiahuaxian").siblings().removeClass("mine-xiahuaxian");
               $ionicSlideBoxDelegate.next();
           }
       }

       $scope.yemianhuadong = function () {
            // $scope.$evalAsync();
           var ss = $(".buttonBar2").offset().top;
           console.log(ss);
           if(ss<=41){
               $(".buttonBar1").css("opacity","1");
               $("h2.yonghu-title").css("opacity","1");
               // $buttonBar2.css(display,"none");
           }
           else if(ss>41){
               $(".buttonBar1").css("opacity","0");
               $("h2.yonghu-title").css("opacity","0");
           }
       }
    });
   